package com.example.demo.org;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @project: cw-org-web-service
 * @description: 对象存储工具类
 * @author: zhaoyujie
 * @create: 2019-02-23 07:37
 */
public class ObjectRelationUtil {

    /**
     * 对象关联
     * @param url
     * @param relationTag
     * @param objUuidList
     * @return
     * @throws Exception
     */
    public static ObjectRelationListResult objectRelation(String url, String relationTag, List<String> objUuidList) throws Exception {
        ObjectRelationParams objectRelationParams = new ObjectRelationParams();
        objectRelationParams.setRelationTag(relationTag);
        objectRelationParams.setUuids(objUuidList);

        String result = HttpUtils.executeJsonParamHttpPost(url, JSON.toJSONString(objectRelationParams));
        if(StringUtils.isEmpty(result)){
            throw new Exception("调用对象关联接口失败");
        }
        ObjectRelationListResult objectRelationListResult = JSONObject.parseObject(result, ObjectRelationListResult.class);
        if(objectRelationListResult.getCode() != 0){
            throw new Exception("调用对象关联接口失败");
        }
        return objectRelationListResult;
    }

    public static ObjectRelationListResult getobjectRelationList(String url) throws Exception {
        String result = HttpUtils.executeHttpGet(url);
        if(StringUtils.isEmpty(result)){
            throw new Exception("调用获取关联对象集合接口失败");
        }
        ObjectRelationListResult objectRelationListResult = JSONObject.parseObject(result, ObjectRelationListResult.class);
        if(objectRelationListResult.getCode() != 0){
            throw new Exception(objectRelationListResult.getMessage());
        }
        return objectRelationListResult;
    }

}
